package com.prs.crm.dao.stock.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.poi.hssf.record.formula.functions.T;
import org.courser.dao.DaoSupportImpl;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import com.prs.crm.context.ToolKits;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.dao.stock.ProductStoreDao;
import com.prs.crm.domain.cdxh.DistributionOrderDetail;
import com.prs.crm.domain.cdxh.cd_Product;
import com.prs.crm.domain.crm.Customer;
import com.prs.crm.domain.stock.Product;
import com.prs.crm.domain.stock.ProductStore;
import com.prs.crm.domain.store.StoreIn;
import com.prs.crm.domain.store.StoreRoom;
import com.prs.crm.service.stock.ProductService;
import com.prs.crm.service.stock.ProductStoreService;
import com.prs.crm.service.stock.StoreInService;
import com.prs.crm.service.stock.StoreRoomService;
import com.sun.istack.internal.FinalArrayList;

public class ProductStoreDaoImpl extends BaseDaoImpl<ProductStore> implements
		ProductStoreDao {
	
	
	private StoreRoomService storeRoomService;
	private ProductService productService;
	
	@SuppressWarnings("unchecked")
	@Override
	public List<ProductStore> getRecord(final int id) {

		return (List<ProductStore>) getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException {
						Criteria criteria = session
								.createCriteria(ProductStore.class);
						criteria.createAlias("product", "p");
						criteria.add(Restrictions.eq("p.id", id));
						return criteria.list();
					}
				});
	}

	@SuppressWarnings("unchecked")
	public ProductStore getStore(final StoreRoom room, final Product product,
			final String batchNum) {

		return (ProductStore) getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException {
						Criteria criteria = session
								.createCriteria(ProductStore.class);
						criteria.add(Restrictions.eq("room", room)).add(
								Restrictions.eq("product", product));
						criteria.add(Restrictions.eq("batchNum", batchNum));
						List<ProductStore> result = (List<ProductStore>) criteria
								.list();

						if (result.size() > 0)
							return result.get(0);
						else
							return null;
					}
				});
	}

	@Override
	public ProductStore getPrStore(Serializable id) {

		return this.getHibernateTemplate().get(ProductStore.class, id);
	}

	@Override
	public void savePrStore(ProductStore pr) {

		this.getHibernateTemplate().saveOrUpdate(pr);
	}

	
	@SuppressWarnings("unchecked")
	@Override
	public List<ProductStore> getProductStoreSize(final int id, final int cid,
			final String batchNum) {
		return (List<ProductStore>) getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException {
						Criteria criteria = session
								.createCriteria(ProductStore.class);
						criteria.createAlias("product", "p");
						criteria.createAlias("room", "r");
						criteria.add(Restrictions.eq("p.id", id));
						criteria.add(Restrictions.eq("r.id", cid));
						criteria.add(Restrictions.eq("batchNum", batchNum));
						return criteria.list();
					}
				});
	}

	@SuppressWarnings("unchecked")
	@Override
	public ProductStore getProductStoreObject(final Product id,
			final StoreRoom room, final String batchNum) {
		return (ProductStore) getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException {
						Criteria criteria = session
								.createCriteria(ProductStore.class);
						criteria.add(Restrictions.eq("product", id)).add(
								Restrictions.eq("room", room)).add(
								Restrictions.eq("batchNum", batchNum));
						List<ProductStore> result = (List<ProductStore>) criteria
								.list();
						if (result.size() > 0) {

							return result.get(0);
						}

						else {

							return null;
						}
					}
				});
	}

	@SuppressWarnings("unchecked")
	@Override
	public String getNum(final Product id, final StoreRoom room,
			final String batchNum) {
		return (String) getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException {
				Criteria criteria = session.createCriteria(ProductStore.class);
				criteria.add(Restrictions.eq("product", id));
				criteria.add(Restrictions.eq("room", room));
				criteria.add(Restrictions.eq("batchNum", batchNum));
				List<ProductStore> slist = criteria.list();

				if (slist.size() == 0) {

					return "21.21.222.33";
				} else {
					double num = 0.0;
					for (int i = 0; i < slist.size(); i++) {
						ProductStore poObject = slist.get(i);
						num = num + poObject.getProductNum();
					}

					return num + "";
				}
			}

		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<ProductStore> getAllProductStores() {

		Criteria criteria = this.getSession()
				.createCriteria(ProductStore.class);
		return criteria.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public double getStoreNums(Product id, StoreRoom room) {
		double d = 0;
		Criteria criteria = this.getSession().createCriteria(ProductStore.class);
		
		criteria.add(Restrictions.eq("product", id)).add(
				Restrictions.eq("room", room));
		List<ProductStore> slist = (List<ProductStore>) criteria.list();
		for (ProductStore productStore : slist) {
			d = d + productStore.getProductNum();
		}
		return d;
	}

	
	@Override
	public double getStoreNums(Product id, StoreRoom room, String batchNum) {
		double d = 0;
		Criteria criteria = this.getSession()
				.createCriteria(ProductStore.class);
		criteria.add(Restrictions.eq("product", id)).add(
				Restrictions.eq("room", room)).add(
				Restrictions.eq("batchNum", batchNum));
		List<ProductStore> slist = (List<ProductStore>) criteria.list();
		for (ProductStore productStore : slist) {
			d = d + productStore.getProductNum();
		}
		return d;
	}

	@Override
	public List<ProductStore> getProductStores(Product product, StoreRoom room) {
		Criteria criteria = this.getSession()
				.createCriteria(ProductStore.class);
		
		criteria.add(Restrictions.eq("product", product)).add(
				Restrictions.eq("room", room));
		criteria.addOrder(Order.asc("recordTime"));//升序
		criteria.addOrder(Order.desc("productNum"));		
		return criteria.list();
	}
	///////////////////////////////////////
	
	
	
	@SuppressWarnings("unchecked")
	@Override
	public List<String> findRoomCodes() {
		// TODO Auto-generated method stub
		return (List<String>)this.getHibernateTemplate().execute(
				new HibernateCallback()
				{
					public Object doInHibernate(Session session) throws HibernateException {
			        	Criteria criteria = session.createCriteria(ProductStore.class);
			        	criteria.setProjection(Projections.distinct(Projections.property("storecode")));
			        	
			        	return criteria.list();
			        }   
				}
		);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ProductStore> findproductNum(final String roomcode) {
		// TODO Auto-generated method stub
		return (List<ProductStore>) this.getHibernateTemplate().execute(
				new HibernateCallback()
				{
					public Object doInHibernate(Session session) throws HibernateException {
			        	Criteria criteria = session.createCriteria(ProductStore.class);
			        	criteria.add(Restrictions.eq("storecode", roomcode));
			     
			        	
						return criteria.list();
			        	
			        	
			        }
				}
		);
	}

	
	@Override
	public void saveStockNums(StoreRoom room) {
		// TODO Auto-generated method stub
		Integer TolNums;
		 List<String> codes= this.findRoomCodes();

		if (codes.size() != 0) {
			for(int i=0;i<codes.size();i++){
				int num=0;
				List<ProductStore>  details=findproductNum(((List<String>) codes).get(i));
				for(int j=0;j<details.size();j++){
					
					 num+=details.get(j).getProductNum();
					
				  }
				
				TolNums=num;
				
				
				room=this.getStoreRoomService().getRoomBycode(codes.get(i));
				
				room.setStockNums(TolNums);
				this.getStoreRoomService().saveOrStoreRoom(room);
				
			}
		}
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Integer> findproductId(final String roomcode) {
		// TODO Auto-generated method stub
		return (List<Integer>)this.getHibernateTemplate().execute(
				new HibernateCallback()
				{
					public Object doInHibernate(Session session) throws HibernateException {
			        	Criteria criteria = session.createCriteria(ProductStore.class);
			        	criteria.add(Restrictions.eq("storecode", roomcode));
			    		criteria.add(Restrictions.gt("productNum",0.0));
			    		criteria.createAlias("product", "p");
			        	criteria.setProjection(Projections.distinct(Projections.property("p.id")));
			        	
			        	return criteria.list();
			        }   
				}
		);
	
	}

	public void setStoreRoomService(StoreRoomService storeRoomService) {
		this.storeRoomService = storeRoomService;
	}

	public StoreRoomService getStoreRoomService() {
		return storeRoomService;
	}

	public ProductService getProductService() {
		return productService;
	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	
	
	

	
	
}
